import time

from flask import Blueprint, request, jsonify, make_response
from flask_restful import Api,Resource
from flask import current_app as app
from uitils.comm import get_data
from db import db1
from uitils.acl import aci
from uitils.jwton import jwte
from uitils.redison import red


userbp = Blueprint('userbp',__name__)



@userbp.route('/login',methods=['POST'])
def login():
    data = get_data()
    # 用户账号与密码
    username = data['username']
    password = data['password']
    # 判断用户名与密码是否正确
    sql = "select * from users where name='{}' and password='{}'".format(username,password)
    res = db1.find(sql)
    if not res:
        # 不存在直接返回
        return jsonify({'code':200,'msg':'用户名或密码错误,请重新输入'})
    starttime = time.time()
    # 生成token
    data = {'data': {"username": res['name'], 'uid': res['id'], "iat": starttime, "exp": 3600}}
    token = jwte.jwt_en(data)
    # 获取用户页面权限
    pagelist = aci.get_page_reslist(res['id'])
    print(pagelist)
    # 获取接口页面权限
    interfacelist = aci.get_interface_reslist(res['id'])
    key = 'interfacelist'+str(res['id'])
    red.set_add(key,interfacelist)
    return jsonify({'code':200,'token':token,'pagelist':pagelist,'userid':res['id'],'username':res['name']})




